from soaplib.wsgi_soap import SimpleWSGISoapApp
from soaplib.service import soapmethod
from soaplib.serializers import primitive as soap_types
from soaplib.serializers import clazz as wrapper
from soaplib.soap import make_soap_fault

from django.conf import settings
from django.http import HttpResponse

class DjangoSoapApp(SimpleWSGISoapApp):

    def __call__(self, request):
        django_response = HttpResponse()
        def start_response(status, headers):
            status, reason = status.split(' ', 1)
            django_response.status_code = int(status)
            for header, value in headers:
                django_response[header] = value


        #if settings.BEHAVIOUR == 2:
        #    raise Exception('behaviour2')

        try:
            response = super(SimpleWSGISoapApp, self).__call__(request.META, start_response)
        except Exception:
            response = make_soap_fault( 'error.soap' )

        django_response.content = "\n".join(response)

        return django_response

